import ReconnectingWebSocket from 'reconnecting-websocket';
import { mapGetters, mapState,mapMutations } from 'vuex';
import { boardlist_new } from '@/api/boardcast_new';
import { escape2Html } from '@/libs/utils';
const webscoket = {
  data() {
    return {
      ws: ''
    };
  },
  computed: {
    ...mapState('user', { 'USER_ID': 'userId', 'nickname': 'nickname' }),
  },
  created() {
    console.log('用户信息-公告', this.nickname, this.USER_ID)
  },
  methods: {
    ...mapMutations('board', [
      'setNotReadCount',
      'setNotReadBoardList',
      'setNewNotice',
      'setChatNotice',
      'setClearNotice',
      'setRefreshTime'
    ]),
    //webScoket推送状态
    webSocketStatus() {
      let _this = this;
      //查询未读数
      let params = {
        pageIndex: 1,
        pageSize: 1,
        lookStatus: 0,
        receiver: this.USER_ID
      };
      boardlist_new(params).then((data) => {
        //	      let data = res.data;
        if (data.code == 1) {
          // commit('setNotReadCount',data.data.total);
          // commit('setNotReadBoardList',data.data.items);
          this.setNotReadCount(data.data.total);
          this.setNotReadBoardList(data.data.items);
        }
      });
      this.setNewNotice({});
      let url = location.host;
      // 总是有把本地代码提交的情况发生，所以这样改一下
      if (process.env.NODE_ENV === 'development') {
        // dev
         //_this.ws = new ReconnectingWebSocket(`ws://172.172.100.1:9001/ws`); //100.1
        _this.ws = new ReconnectingWebSocket(`ws://172.25.0.20:9001/ws`); //30.16
       // _this.ws = new ReconnectingWebSocket(`ws://172.25.0.30:9001/ws`); //30.25
      } else {
        // build  除非后台接口变了，否则不需要动这里
        _this.ws = new ReconnectingWebSocket(`ws://${url}/wss/ws`);
      }
      _this.ws.addEventListener('open', () => {
        _this.ws.send(JSON.stringify({ type: 'CONNECT', userId: `${_this.USER_ID}` }));
        console.log(
          'client：打开连接,获取演练状态推送这个是新的-------------------' + _this.USER_ID + this.nickname, _this.ws
        );
      });
      _this.ws.addEventListener('message', (evt) => {

          let received_msg = JSON.parse(evt.data);
          // 感觉就是没有给消息
          console.log('ws-client：-------message------------' + received_msg);
          if (received_msg.pushType == 1) {
          //公告内容
          _this.$Notice.success({
            title: '新公告',
            desc: received_msg.data.noticeMsg.content,
            duration: 30
          });
          _this.setNewNotice(received_msg.data.noticeMsg);
        }
        //查询未读数
        let params = {
          pageIndex: 1,
          pageSize: 1,
          lookStatus: 0,
          receiver: this.USER_ID
        };
        boardlist_new(params).then((data) => {
          //        let data = res.data;
          if (data.code == 1) {
            // commit('setNotReadCount',data.data.total);
            // commit('setNotReadBoardList',data.data.items);
            _this.setNotReadCount(data.data.total);
            _this.setNotReadBoardList(data.data.items);
          }
        });
        if (received_msg.pushType == 2) {
          //        _this.dealTaskDetailList.forEach((item,index)=>{
          //          let curStatus = received_msg.data.sceneInfo.progress;
          //          if(item.id===received_msg.data.sceneInfo.id){
          //            _this.$set(_this.dealTaskDetailList[index],'progress',curStatus);
          //          }
          //        })
        }
        if (received_msg.pushType == 2 || received_msg.pushType == 6) {
          this.setRefreshTime(new Date().getTime());
        }
        if (received_msg.pushType == 4) {
          //聊天信息
          _this.setChatNotice(received_msg.data.chatMsg);
        }
        if (received_msg.pushType == 7) {
          //清空场景消息和公告
          _this.setClearNotice(received_msg.data);
        }
      });
      _this.ws.addEventListener('close', () => {
        console.log('close');
      });
      _this.ws.addEventListener('error', () => {
        console.log('error');
      });
    }
  },
  beforeDestroy() {
    this.setNotReadCount(0);
    this.setNotReadBoardList([]);
    this.setNewNotice({});
    if (this.ws) {
      this.ws.close();
    }
  },
  destroyed() {}
};

// 不再限制空格输入
// window.onkeypress = (event) => {
//   if (event.keyCode == 32) event.returnValue = false;
// }

export default webscoket;
